package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public UserBean login(String userName, String pwd, HttpServletRequest request) {
        UserBean userBean = userService.login(userName, pwd);
        if (userBean != null) {
            request.getSession().setAttribute("loginUser", userBean);
        }
        return userBean;
    }

    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return user;
    }

    @RequestMapping("add")
    public String add(UserBean user, Integer[] roleIdArray) {
        userService.add(user, roleIdArray);
        return "ok";
    }

    @RequestMapping("findByItem")
    public IPage<UserBean> findByItem(Integer pageNO, String userName) {
        return findByItem(pageNO, userName);
    }

    @RequestMapping("updatePwd")
    private void updatePwd(Integer id, String pwd) {
        userService.updatePwd(id, pwd);
    }

    @RequestMapping("del")
    public String del(Integer id, UserBean loginUser) {
        userService.del(id, loginUser);
        return "ok";
    }

    @RequestMapping("findById")
    public UserBean findById(Integer id) {
        return userService.findById(id);
    }
    @RequestMapping("updateRoles")
   public void updateRoles(Integer id,Integer[] rolesId){
        updateRoles(id,rolesId);
   }
}
